import re
from pprint import pprint

import requests
from lxml import etree
from .db import RedisClient
from .crawler import Crawler
from .setting import *
import sys


class Getter(object):
    def __init__(self):
        self.redis = RedisClient()
        self.crawler = Crawler()

    def is_over_threshold(self):
        """
        判断是否达到了代理池限制
        """
        if self.redis.count() >= POOL_UPPER_THRESHOLD:
            return True
        else:
            return False

    def get_address(self, proxy):
        """
        获取代理的位置
        :param proxy: 代理
        :return: address
        """
        ip = proxy.split(":")[0]
        data = {
            "ip": ip
        }
        url = "http://ip.tool.chinaz.com"
        response = requests.post(url, data=data)
        html = etree.HTML(response.text)
        address = html.xpath('//p[@class="WhwtdWrap bor-b1s col-gray03"]/span[4]/text()')[0].split(" ")[0]
        return address

    def get_address_code(self, address):
        """
        根据代理位置获得区域编码
        :param address:
        :return: code
        """
        # 省级
        print(address)
        if re.match(r"^\w*省", address):
            # 市级
            if re.match(r"^\w*省\w*市", address):
                address = re.match(r"^\w*省\w*市", address).group()
                code = self.redis.get_code(address)
                print(code)
                return code
            else:
                address = re.match(r"^\w*省", address).group()
                code = self.redis.get_code(address)
                return code

        elif re.match(r"^\w*自治区", address):
            if re.match(r"^\w*自治区\w*市", address):
                address = re.match(r"^\w*自治区\w*市", address).group()
                code = self.redis.get_code(address)
                return code
            else:
                address = re.match(r"^\w*自治区", address).group()
                code = self.redis.get_code(address)
                return code

        elif re.match(r"^\w*市", address) and not re.match(r"^\w*省\w*市", address):
            if re.match(r"^\w*市\w区", address):
                address = re.match(r"^\w*市\w区", address).group()
                code = self.redis.get_code(address)
                return code
            else:
                address = re.match(r"^\w*市", address).group()
                code = self.redis.get_code(address)
                return code
        else:
            pass

    def run(self):
        print('获取器开始执行')
        if not self.is_over_threshold():
            for callback_label in range(self.crawler.__CrawlFuncCount__):
                callback = self.crawler.__CrawlFunc__[callback_label]
                # 获取代理
                proxies = self.crawler.get_proxies(callback)
                sys.stdout.flush()
                for proxy in proxies:
                    # 获取代理地址
                    address = self.get_address(proxy)
                    # 获取地址码值
                    code = self.get_address_code(address)
                    if code:
                        self.redis.add(proxy, code)
